﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MenuLib;

namespace MenuBuilder
{
    class MenuBuilderMenu
    {
        private Menu menu;
        private IMenuBuilder menuBuilder;

        public MenuBuilderMenu(IMenuBuilder builder)
        {
            menuBuilder = builder;

            BuildMyMenu();
        }

        public void Run()
        {
            menu.Run();
        }

        private void BuildMyMenu()
        {
            menu = new Menu();
            menu.Text = "== MENU BUILDER ==";

            MenuHotKeyedItem item = new MenuHotKeyedItem(
                "Add", "_Add new menu item", 'a');
            item.Select += new EventHandler(add_Select);
            menu.Items.Add(item);

            item = new MenuHotKeyedItem(
                "Insert", "_Insert new menu item", 'i');
            item.Select += new EventHandler(insert_Select);
            menu.Items.Add(item);

            item = new MenuHotKeyedItem(
                "Display", "_Display menu currently built", 'd');
            item.Select += new EventHandler(display_Select);
            menu.Items.Add(item);

            item = new MenuHotKeyedItem(
                "Remove", "_Remove menu item", 'r');
            item.Select += new EventHandler(remove_Select);
            menu.Items.Insert(2, item);

            item = new MenuHotKeyedItem(
                "Save", "_Save the built menu", 's');
            item.Select += new EventHandler(save_Select);
            menu.Items.Add(item);
        }

        void add_Select(object sender, EventArgs e)
        {
            menuBuilder.Add(false);
        }

        void insert_Select(object sender, EventArgs e)
        {
            menuBuilder.Add(true);
        }

        void remove_Select(object sender, EventArgs e)
        {
            menuBuilder.Remove();
        }

        void display_Select(object sender, EventArgs e)
        {
            Console.WriteLine("\n!!!!!!! Menu Currently Built: !!!!!!!");
            menuBuilder.Product.Display();
            Console.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

            Console.Write("Press any key to continue...");
            Console.ReadKey(true);
        }

        void save_Select(object sender, EventArgs e)
        {
            while (true)
            {
                Console.Write("Type the file name to save the menu to: ");
                string path = Console.ReadLine();

                if (path == string.Empty)
                {
                    Console.WriteLine("File name can't be empty");
                    continue;
                }

                menuBuilder.Product.Save(path);
                break;
            }
        }
    }
}
